package com.kaibes.core.spring.utils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class PropertyUtils extends com.kaibes.core.base.util.PropertyUtils {

	@Autowired
    private ObjectMapper objectMapper;
    private static PropertyUtils instance;
    
    public PropertyUtils() {
    	PropertyUtils.instance = this;
    }
    
    public static PropertyUtils getInstance() {
		return instance;
	}
    
	public JsonNode readAsJsonFromResources(String path) throws IOException {
		ClassPathResource source = new ClassPathResource(path);
		return readAsJson(source.getInputStream());
    }
	
	public JsonNode readAsJson(InputStream io) throws IOException {
		Properties properties = getProperties(io);
        String json = objectMapper.writeValueAsString(properties);
        JsonNode node = objectMapper.readTree(json);
        return node;
    }
	
}
